package com.example.demo.manage;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Kafka消息生产类
 * @Author: wn
 * @Date: 2021/8/3 13:34
 * @Version 1.0
 * @Note
 */
@Log
@Component
public class KafkaProducer {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${kafka.topic.user}")
    private String topicUser;//topic名称

    public void sendUserMessage(String msg) {
//        GsonBuilder builder = new GsonBuilder();
//        builder.setPrettyPrinting();
//        builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
//        String message = builder.create().toJson(user);
        kafkaTemplate.send(topicUser, msg);
        log.info("\n生产消息至Kafka\n" + msg);
    }
}
